package cn.lyq.card.utils

import android.net.Uri
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import cn.lyq.card.R
import cn.lyq.card.data.model.SimpleTabEntity
import cn.lyq.card.global.Const
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.flyco.tablayout.CommonTabLayout
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import java.util.*


object ViewClickTime {
    var lastClickTime = 0L
    const val SPACE_TIME = 500
}

const val key = 10 shl 24

fun getVisibility(isGone: Boolean) = if (isGone) View.GONE else View.VISIBLE

fun showResult(view: TextView, loading: View, message: String? = null) {
    val isLoading = message == null

    if (isLoading) {
        view.setTag(key, view.text.toString())
        view.text = ""
    } else {
        view.getTag(key).takeIf { it is String }?.let { view.text = it as String }
    }
    loading.visibility = if (isLoading) View.VISIBLE else View.GONE
    message?.takeIf { it.isNotEmpty() }?.let {
        ToastUtils.showShort(it)
    }
}

fun View.setClick(clickAction: () -> Unit) {
    this.setOnClickListener {
        val time = System.currentTimeMillis()
        if (time - ViewClickTime.lastClickTime >= ViewClickTime.SPACE_TIME) {
            ViewClickTime.lastClickTime = time
            clickAction()
        }
    }
}

fun ImageView.loadUrl(url: String?) {
    Glide.with(this).load(url?.toImgUrl() ?: "").error(R.drawable.ic_data_empty).into(this)
}
fun ImageView.loadUrl(uri: Uri?) {
    Glide.with(this).load(uri).error(R.drawable.ic_data_empty).into(this)
}

fun RecyclerView.ViewHolder.setClick(view: View, clickAction: () -> Unit) {
    view.setOnClickListener {
        val time = System.currentTimeMillis()
        if (time - ViewClickTime.lastClickTime >= ViewClickTime.SPACE_TIME
            && adapterPosition != RecyclerView.NO_POSITION
        ) {
            ViewClickTime.lastClickTime = time
            clickAction()
        }
    }
}

fun CommonTabLayout.setSimpleData(titles: Array<String>) {
    val data = arrayListOf<CustomTabEntity>()
    titles.forEach {
        data.add(SimpleTabEntity(it))
    }
    setTabData(data)
    currentTab = 0
}

fun CommonTabLayout.setOnTabSelected(block: ((Int) -> Unit)) {
    setOnTabSelectListener(object : OnTabSelectListener {
        override fun onTabSelect(p0: Int) {
            block(p0)
        }

        override fun onTabReselect(p0: Int) {
        }

    })
}

fun CommonTabLayout.bindViewPager(viewPager: ViewPager2, block: ((Int) -> Unit)? = {}) {
    setOnTabSelectListener(object : OnTabSelectListener {

        override fun onTabSelect(p0: Int) {
            viewPager.setCurrentItem(p0, true)
            block?.invoke(p0)
        }

        override fun onTabReselect(p0: Int) {
        }

    })
    viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            if (position != currentTab) {
                currentTab = position
            }
            block?.invoke(position)
        }
    })
}

fun CommonTabLayout.bindViewPager(viewPager: ViewPager, block: ((Int) -> Unit)? = {}) {
    setOnTabSelectListener(object : OnTabSelectListener {

        override fun onTabSelect(p0: Int) {
            viewPager.setCurrentItem(p0, true)
            block?.invoke(p0)
        }

        override fun onTabReselect(p0: Int) {
        }

    })
    viewPager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
        }

        override fun onPageSelected(position: Int) {
            if (position != currentTab) {
                currentTab = position
            }
            block?.invoke(position)
        }

        override fun onPageScrollStateChanged(state: Int) {
        }

    })
}

fun Number.toMoney() = String.format(Locale.CHINA, "%.2f", this)

fun String.toBankNo(): String {
    var temp = ""
    var start = 0
    var end = 4
    while (start < length) {
        temp = "${temp}${substring(start, end.coerceAtMost(length))} "
        start = end
        end += 4
    }
    return temp
}

fun String.toImgUrl(): String {
    return if (this.startsWith("http")) {
        this
    } else {
        "${Const.BASE_URL}${this}"
    }
}

fun <E> List<E>.hasIndex(index: Int) = index in 0 until size
fun <E> Array<E>.hasIndex(index: Int) = index in 0 until size